<?php if (true){ ?>
<?php  

error_reporting(0);
$portlet_title="portlet_contenu_Title_".$LANG; 
//$currentPortletId=$currentPortletId;
if ($portlet_contenu_Page!=null) $system_pageName=$portlet_contenu_Page;
//if (isset($portlet_contenu_PortletId)) $system_PortletId=$portlet_contenu_PortletId;


if (getParameter("portletId")!=null) $PortletId=getParameter("portletId");
//




if ($debug==true) $system_PortletInstanceId;
//echo "ok".$PortletId." ".$system_PortletInstanceId;
if (getParameter("content")!=null && isset($PortletId) && $PortletId==$system_PortletInstanceId)
{

//Check Portlet Id
	$portlet_contenu_contentName=getParameter("content");
  //  echo $portlet_contenu_contentName;
  

	
	if (getParameter("RemotePresentationTemplate")!=null && getParameter("RemotePresentationTemplate")!="") $portlet_contenu_presentationTemplate=getParameter("RemotePresentationTemplate");
}
//echo "system ".$system_PortletId." <br/>";
//echo "pref ".$portlet_contenu_PortletId." pref<br/>";
/*
function TryGetRecordCount($sSql) {
	$cnt=0;
	if ($rs = $GLOBALS["conn"]->Execute($sSql)) {
		if (!$rs->EOF && $rs->FieldCount() > 0) {²
			$cnt = $rs->fields[0];
			$rs->Close();
		}
	}
	return intval($cnt);
}*/



/*

if (getParameter("presentationTemplate")!=null)) {
	$tmp=$portlet_contenu_presentationTemplate;
	$portlet_contenu_presentationTemplate=getParameter("presentationTemplate");
}*/

	//TODO display an error if the presentation Template does not exist. 


if ($portlet_contenu_presentationTemplate!=null){


$presentationTemplate=getPresentatationTemplate($portlet_contenu_presentationTemplate,"contenu","E");
}else{
	
	ShowError($var_portal_nopresentationtemplate);
}

//echo $sSql."<br/>";


$contentCount=0;


$Limit="";

if ($portlet_contenu_Limit!=null && $portlet_contenu_Limit!="") $Limit="LIMIT  0 , ".$portlet_contenu_Limit;

if (isset($portlet_contenu_ContentPerPage) && $portlet_contenu_ContentPerPage!=null && $portlet_contenu_ContentPerPage!="") {
	if (getParameter("start")!=null) $start=getParameter("start")*$portlet_contenu_ContentPerPage; else $start=0;
	$Limit="LIMIT  ".$start." , ".$portlet_contenu_ContentPerPage;
}

$sSql ="";
    $innertags="";
    $tagswhere="";
if ($portlet_contenu_contentName!=null &&  $portlet_contenu_contentName!="") {

$sSql ="Select distinct(content.id) From portal_content content Where content.portalId=$portalId and content.name = '$portlet_contenu_contentName'";
}else{

/*
    echo("<pre>\n");
    print_r($_REQUEST);
    echo("</pre>\n");

*/

    if ($portlet_contenu_Tags!=null && $portlet_contenu_Tags!=""){

       if ($portlet_contenu_Tags=="#queryString#") {
       	
      
       	$portlet_contenu_Tags=getParameter("tag");
       	
       	if ($portlet_contenu_Tags==null) {
       		$portlet_contenu_Tags=getParameter("content");
       	}
       	
       	
       }
      
        	
        	

        $innertags="INNER JOIN portal_tag_content ON portal_tag_content.contentId = content.id
INNER JOIN portal_tags ON portal_tags.id = portal_tag_content.tagId";
        /*  $sSql="Select distinct(content.id)
            From portal_content content Inner Join
            portal_contentvalues contentvalues On content.id = contentvalues.contentId Inner Join
            portal_authoringtemplate_elements authoringtemplate_elements On contentvalues.authoringtemplate_elementId =
            authoringtemplate_elements.id Inner Join
            portal_tag_content tag_content On content.id = tag_content.contentId Inner Join
            portal_tags tags On tag_content.tagId = tags.id
            Where content.portalId = ".$portalId." and tags.name = '".$portlet_contenu_Tags."' and ( workflow_statusId=2 ) order by content.date desc ".$Limit;
        */
        $tagswhere=" UPPER(portal_tags.name) = UPPER('$portlet_contenu_Tags') and ";
    }


    if (isset($portlet_contenu_Type) && $portlet_contenu_Type!=""){
        $sSql="Select Distinct (content.id)
From portal_content content Inner Join
  portal_authoringtemplate On content.authoringtemplateId =
    portal_authoringtemplate.id $innertags
Where portal_authoringtemplate.portalId=$portalId and  $tagswhere portal_authoringtemplate.name = '".$portlet_contenu_Type."'";

    }


}

$emptyQuery=true;
if ($sSql!=""){
    $sSql .=" And content.portalId = ".$portalId." and ( workflow_statusId=2 ) Order By content.date Desc   ".$Limit;
//echo $sSql;


$rs=$GLOBALS["conn"]->Execute($sSql);
/*
$start=3;
$end=$start+$portlet_contenu_ContentPerPage;
&& $i<$portlet_contenu_ContentPerPage
$i=0;*/
$nSQL=explode("LIMIT", $sSql);

if ($debug) echo $nSQL[0];
$maxContent=$GLOBALS["conn"]->Execute($nSQL[0])->RecordCount(); 


//Pagination.
if ($portlet_contenu_ContentPerPage!=null && $portlet_contenu_ContentPerPage!=""){
if (getParameter("start")!=null) $start=getParameter("start"); else $start=0;
$maxPage=round($maxContent/$portlet_contenu_ContentPerPage)-1;
$next=$start+1; if ($next>$maxPage) $next=$start;
$previous=$start-1; if ($previous<0) $previous=0;
$pagination_next=createUrl("start",$next);
$pagination_previous=createUrl("start",$previous);
$pagination_first=createUrl("start",0);
$pagination_last=createUrl("start",$maxPage);
}


//Get count content to display
$contentCount=$rs->RecordCount();

//Display header
if ($portlet_contenu_Header!=null && $contentCount>0) {
	//echo $portlet_contenu_Header;
//	echo "<script> alert('".getPresentatationTemplate($portlet_contenu_Header,"contenu","H")."'); </script>";
	eval (getPresentatationTemplate($portlet_contenu_Header,"contenu","H"));
	$portlet_contenu_Header="";
}

$currentLang="_".$LANG."_";


$index=0;
while (!$rs->EOF )  {
	$index++;


	
	//$include_comment="\".eval('include('comment.php')').\"";
	/*$i++;
	if ($start<$end && !$rs->EOF) $rs->MoveNext();*/
	
	$emptyQuery=false;
	
	$contentId=$rs->fields["id"];

    $canView=canView("portal_content",$contentId);
    if ($canView){

$sSql="Select content.name As contentName,
  contentvalues.TextField".$currentLang."value as TextFieldvalue,
  contentvalues.TextArea".$currentLang."value as TextAreavalue,
  contentvalues.RichText".$currentLang."value as RichTextvalue,
  DATE_FORMAT(contentvalues.Datevalue,  '%d/%m/%Y') as Datevalue,
  contentvalues.Imagevalue as Imagevalue,
  contentvalues.Image_arvalue as Imagearvalue,
   contentvalues.InternalLinkvalue as InternalLinkvalue ,
   contentvalues.Queryvalue as Queryvalue ,
   contentvalues.Flashvalue as Flashvalue ,
   contentvalues.FlashWidthvalue as FlashWidthvalue ,
   contentvalues.FlashHeightvalue as FlashHeightvalue ,
   contentvalues.FlashVarsvalue as FlashVarsvalue ,
   contentvalues.YoutubeVideovalue as YoutubeVideovalue,
    contentvalues.YoutubeVideowidthvalue as YoutubeVideowidthvalue,
     contentvalues.YoutubeVideoheightvalue as YoutubeVideoheightvalue,
      contentvalues.Filevalue as Filevalue,
  authoringtemplate_elements.name As Name,
  CONCAT(Map_Longvalue, '/', Map_Latvalue) as Mapvalue,
  authoringelement.name As Type
From portal_content content Inner Join
 portal_contentvalues contentvalues On content.id = contentvalues.contentId Inner Join
 portal_authoringtemplate_elements authoringtemplate_elements On contentvalues.authoringtemplate_elementId =
    authoringtemplate_elements.id Inner Join
  portal_authoringelement authoringelement On authoringelement.id = authoringtemplate_elements.elementId
Where   content.id =$contentId";
//echo $sSql."<br/>";

    if ($mode=="edit"){

$url=addParameter("table", "portal_content","/butterflyportal/");
$url=addParameter("ButterflyPortal_sys_rec", $contentId,$url);
$url=addParameter("ButterflyPortal_sys_operation", "Change",$url);

        ?>
        <div class="content_edit">
    <a target="_blank" class="edit" href="<?=$url?>"><img src="/images/icons/edit.png" ></a>
        <?php

    }

      $rs2=$GLOBALS["conn"]->Execute($sSql);

      $unregister_vars="";
while (!$rs2->EOF)  {


	$type=$rs2->fields["Type"];
	$system_contentName=$rs2->fields["contentName"];
	$contentName=$rs2->fields["contentName"];





    $value=str_replace("\"","\\\"",$rs2->fields[$rs2->fields["Type"]."value"]);

    if ($value=="null") $value="";
	if ($type=="InternalLink") {
		$sSql="Select portal_pages.name
		From portal_pages
		Where portal_pages.id = '".$value."' And portal_pages.portalId = '".$portalId."' And
  		portal_pages.active = 'Y'  ";
		$rs3=$GLOBALS["conn"]->Execute($sSql);
		//echo $sSql;
		$value="?pageName=".$rs3->fields["name"];
	}
	if ($type=="Flash") {
		$timer= rand_string( 10 );
$value="<div id='Flash_".$timer."' style='width:".$rs2->fields["FlashWidthvalue"]."px;height:".$rs2->fields["FlashHeightvalue"]."px'></div><script type='text/javascript'>


var params = {
 src : '/".getFilesPath("image")."/".$value."',
  wmode: 'transparent',
    menu: 'false',
   ".$rs2->fields["FlashVarsvalue"]."
};
//swfobject.embedSWF('/contentimages/".$value."', 'Flash_".$timer."', '".$rs2->fields["FlashWidthvalue"]."', '".$rs2->fields["FlashHeightvalue"]."', '9.0.0','expressInstall.swf', flashvars, params, null);
 flashembed('Flash_".$timer."', params);
</script>";
	}elseif ($type=="Image")
	{
       if ($lang=="ar" && $rs2->fields[$rs2->fields["Type"].$lang."value"]!=""){
           $value=getFileManagerImage($rs2->fields[$rs2->fields["Type"].$lang."value"]);
       }else
		$value=getFileManagerImage($rs2->fields[$rs2->fields["Type"]."value"]);


	}elseif ($type=="YoutubeVideo")
    {
        $timer= rand_string( 10 );
        $width=$rs2->fields[$rs2->fields["Type"]."widthvalue"];
        $height=$rs2->fields[$rs2->fields["Type"]."heightvalue"];
        $value="<div id='youtube-player-container_".$timer."'> </div><script>

        $('#youtube-player-container_".$timer."').tubeplayer({
	width: ".$width.", // the width of the player
	height: ".$height.", // the height of the player
	allowFullScreen: 'true', // true by default, allow user to go full screen
	initialVideo: '".$rs2->fields[$rs2->fields["Type"]."value"]."', // the video that is loaded into the player
	preferredQuality: 'default',// preferred quality: default, small, medium, large, hd720
	onPlay: function(id){}, // after the play method is called
	onPause: function(){}, // after the pause method is called
	onStop: function(){}, // after the player is stopped
	onSeek: function(time){}, // after the video has been seeked to a defined point
	onMute: function(){}, // after the player is muted
	onUnMute: function(){} // after the player is unmuted
});

        </script>";
    }elseif ($type=="File")
    {

        if ($rs2->fields[$rs2->fields["Type"]."value"]!="")
        $value="/".getFilesPath("files")."/".$rs2->fields[$rs2->fields["Type"]."value"];
        else
            $value="";
    }

	eval("$".$rs2->fields["Name"]."=\"".$value."\";");
	$unregister_vars.="$".$rs2->fields["Name"]."=\"null\";";
		 	
$rs2->MoveNext();
}

	//echo "<script>alert('".$presentationTemplate."')</script>";

ini_set('display_errors', '0');


eval($presentationTemplate);

ini_set('display_errors', '1');

eval($unregister_vars);


    if ($mode=="edit"){
        ?>
   </div>

        <?php

    }

    }

$rs->MoveNext();


//if (isset($portlet_contenu_allowComments) && $portlet_contenu_allowComments=="Yes") include 'comment.php';


//Display Separator
if ($portlet_contenu_presentationTemplate!=null && !$rs->EOF && $portlet_contenu_Separator!=null && $canView ) {
	//echo $portlet_contenu_Header;
	eval (getPresentatationTemplate($portlet_contenu_Separator,"contenu","S"));
	$portlet_contenu_Separator="";
}


  
 }}
 
 if ($portlet_contenu_presentationTemplate!=null && $portlet_contenu_Footer!=null  && $contentCount>0) {
 	
 	//echo "<script> alert('".getPresentatationTemplate($portlet_contenu_Footer,"contenu","F")."'); </script>";
	eval (getPresentatationTemplate($portlet_contenu_Footer,"contenu","F"));
 	$portlet_contenu_Footer="";
 }
if ($emptyQuery){

    if (strtolower($portlet_contenu_hideErrors)!="yes") ShowError($var_portal_nocontent);
}
$portlet_contenu_ContentPerPage=null;

$portlet_contenu_Separator=null;

$portlet_contenu_allowComments=null;
?>
<?php }



if ($mode=="edit") {
if ($portlet_contenu_Type!=null && $portlet_contenu_Type!=""){

$url=addParameter("table", "portal_content","/butterflyportal/");
$url=addParameter("ButterflyPortal_sys_rec", getObjByKey("portal_authoringtemplate","id","name",$portlet_contenu_Type,"portalId",$portalId),$url);
$url=addParameter("ButterflyPortal_sys_operation", "Add",$url);

    ?>
<a title="Add Content" target="_blank" href="<?=$url?>" ><div class="button" ><img src="/butterflyportal/images/menu/addcontent.png"/><?php echo getObjByKey("portal_authoringtemplate","lib","name",$portlet_contenu_Type,"portalId",$portalId); ?></div></a>
<?php
}






$url=addParameter("table", "portal_presentationtemplate","/butterflyportal/");
$url=addParameter("ButterflyPortal_sys_rec", getObjByKey("portal_presentationtemplate","id","name",$portlet_contenu_presentationTemplate,"portalId",$portalId),$url);
$url=addParameter("ButterflyPortal_sys_operation", "Change",$url);
?>

<a  target="_blank" title="Edit Presentation Template"  href="<?=$url?>"><div class="button" ><img src="/butterflyportal/images/presentation.png"/><?php echo getObjByKey("portal_presentationtemplate","lib","name",$portlet_contenu_presentationTemplate,"portalId",$portalId); ?></div></a>
    <?php }
    
    
    error_reporting(E_ALL);
?>